﻿using System;
using ExFlash.Filters;
using ExFlash.Tags;

namespace ExFlash.Records
{
    public class ButtonRecord : ISwfSerializable
    {
        private readonly TagCodes _code;

        public Boolean ButtonHasBlendMode
        {
            get;
            protected set;
        }

        public Boolean ButtonHasFilterList
        {
            get;
            protected set;
        }

        public Boolean ButtonStateHitTest
        {
            get;
            protected set;
        }

        public Boolean ButtonStateDown
        {
            get;
            protected set;
        }

        public Boolean ButtonStateOver
        {
            get;
            protected set;
        }

        public Boolean ButtonStateUp
        {
            get;
            protected set;
        }

        public ushort CharacterID
        {
            get;
            protected set;
        }

        public ushort PlaceDepth
        {
            get;
            protected set;
        }

        public MatrixRecord PlaceMatrix
        {
            get;
            protected set;
        }

        public CXFormAlphaRecord ColorTransform
        {
            get;
            protected set;
        }

        public FilterList FilterList
        {
            get;
            protected set;
        }

        public BlendModes BlendMode
        {
            get;
            protected set;
        }

        public ButtonRecord(TagCodes code)
        {
            _code = code;
        }
        
        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            // Reserved
            reader.SkipBits(2);

            ButtonHasBlendMode = reader.ReadBoolean();
            ButtonHasFilterList = reader.ReadBoolean();
            ButtonStateHitTest = reader.ReadBoolean();
            ButtonStateDown = reader.ReadBoolean();
            ButtonStateOver = reader.ReadBoolean();
            ButtonStateUp = reader.ReadBoolean();

            reader.AlignBits();

            CharacterID = reader.ReadUInt16();

            PlaceDepth = reader.ReadUInt16();

            PlaceMatrix = new MatrixRecord();
            PlaceMatrix.SwfDeserialize(reader);

            if (_code != TagCodes.DefineButton)
            {
                ColorTransform = new CXFormAlphaRecord();
                ColorTransform.SwfDeserialize(reader);
                
                if (ButtonHasFilterList)
                {
                    FilterList = new FilterList();
                    FilterList.SwfDeserialize(reader);
                }

                if (ButtonHasBlendMode)
                {
                    BlendMode = (BlendModes)reader.ReadByte();
                }
            }
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
